package oh.june.kwon.soapClient;

import java.io.IOException;
import java.rmi.ServerException;
import java.util.Vector;

import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.WLANInfo;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.ksoap2.transport.ServiceConnection;
import org.xmlpull.v1.XmlPullParserException;

public class WebServiceCore {
	
	private static final String WEB_SERVICE_NAMESPACE = "http://iconlab.co.kr/";
	
	private String _methodName;
	private String _methodURL; 
	private SoapObject _rpc;
	private Vector _requestProperty;

	public WebServiceCore(String methodURL, String methodName)
	{
		_requestProperty = new Vector();
		_methodURL = methodURL;
		_methodName = methodName;
		_rpc = new SoapObject(WEB_SERVICE_NAMESPACE, _methodName);
		
	}
	
	public void addProperty(String name, Object value){
		_rpc.addProperty(name, value);
	}
	
	public void setRequestProperty(String name, String value){
		_requestProperty.addElement(new String[] {name, value});
	}
	
	public Object get() throws ServerException
	{
		HttpTransport ht = null;
		try {
			String serviceUrl = _methodURL;
			String soapAction = WEB_SERVICE_NAMESPACE + _methodName;
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			
			envelope.bodyOut = _rpc;
			envelope.dotNet = true;
			envelope.encodingStyle = SoapEnvelope.XSD;
			
			if(DeviceInfo.isSimulator()){
			    serviceUrl = serviceUrl + ";deviceSide=true";
			}
		    else if(DeviceInfo.isSimulator()==false) {
		          if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
		           serviceUrl+=";deviceside=true;interface=wifi";
		          } else {
		           serviceUrl+=";deviceside=true;apn=web.sktelecom.com";          
		          }
	        }
			
			ht = new HttpTransport(serviceUrl){
				protected ServiceConnection getServiceConnection() throws java.io.IOException
				{
					ServiceConnection connection = super.getServiceConnection();
					int requestPropertySize = _requestProperty.size();
					for(int i = 0 ; i < requestPropertySize ; i ++)
					{
						String[] property = (String[])_requestProperty.elementAt(i);
						connection.setRequestProperty(property[0], property[1]);
					}
					
					return connection;
				}
			};
			
			ht.debug = false;
			ht.call(soapAction, envelope);
			
			
			Object responseBody = envelope.getResponse();
			System.out.println("success call-" + _methodName);
			return responseBody;
			
		} catch (Exception e) {
			
			System.out.println("------ exception (" + _methodName + ") ----------");
			if(ht != null)
			{
				System.out.println("request=" + ht.requestDump);
				System.out.println("response=" + ht.responseDump);
			}
			System.out.println(e.toString());
			System.out.println(e.getMessage());
			e.printStackTrace();
			
			throw new ServerException(_methodName, e);
			
		}
	}
	
	public SoapDataSet getDataSet() throws ServerException {
		
		SoapObject responseBody = (SoapObject)get();
		SoapObject datasetBody = null;
		try{
			responseBody = (SoapObject) responseBody.getProperty(1);
			datasetBody = (SoapObject)responseBody.getProperty(0);
		}catch(ArrayIndexOutOfBoundsException aioobe){
			return null;
		}
		return new  SoapDataSet(datasetBody);
	}
	
	public SoapDataSet[] getDataSetWithResult() throws ServerException {
		SoapDataSet[] soapDataSetArr = new SoapDataSet[2];
		
		SoapObject responseBody = (SoapObject)get();
		SoapObject resultSetBody = null; //resultSoapObject
		SoapObject datasetBody = null;	//dataSetSoapObject 	
		try{			
			resultSetBody = (SoapObject) responseBody.getProperty(0);
			resultSetBody = (SoapObject)resultSetBody.getProperty(0);
			
			datasetBody = (SoapObject) responseBody.getProperty(1);
			datasetBody = (SoapObject)datasetBody.getProperty(0);
		}catch(ArrayIndexOutOfBoundsException aioobe){
			return null;
		}
		soapDataSetArr[0] = new SoapDataSet(resultSetBody);
		soapDataSetArr[1] = new SoapDataSet(datasetBody);
		return soapDataSetArr;
	}
	
}
